# syntax = docker/dockerfile:experimental
FROM maven:3-openjdk-17-slim as builder
WORKDIR /application
COPY pom.xml .
RUN --mount=type=cache,target=/root/.m2,id=maven-repo,sharing=locked \
    mvn -B dependency:resolve
COPY . .
RUN --mount=type=cache,target=/root/.m2,id=maven-repo,sharing=locked \
    mvn -B -DskipTests=true clean install

FROM azul/zulu-openjdk-alpine:17-jre as artifact
WORKDIR /application
ARG JAR_FILE=target/*.jar
COPY --from=builder /application/${JAR_FILE} application.jar
RUN java -Djarmode=layertools -jar application.jar extract

FROM azul/zulu-openjdk-alpine:17-jre
EXPOSE 8080
WORKDIR /application
COPY --from=artifact /application/dependencies/ ./
COPY --from=artifact /application/spring-boot-loader/ ./
COPY --from=artifact /application/snapshot-dependencies/ ./
COPY --from=artifact /application/application/ ./
ENTRYPOINT ["java", "org.springframework.boot.loader.JarLauncher"]